<?php
class SiteController extends Zend_Controller_Action
{

	public function init()
	{
		// Set lang to view
		$this->view->lang = $this->_request->getParam ( 'lang', 'en' );

		// Set actions for ajax
		$ajaxContext = $this->_helper->getHelper ( 'AjaxContext' );
		$ajaxContext->addActionContext ( 'getProjectImages', 'json' )->initContext ();
	}

	public function homeAction()
	{
		$date = new Zend_Date();
		$this->view->date = $date;
			
		$mdlTips = new Model_Tips();
		
		// get current tip
		$this->view->tip = $mdlTips->getList( "MONTH(`date`) = " . $date->get("M") . " AND `result` IS NULL ", "date ASC", 1, 1 )->toArray ();
		
		// get list record tips
		$this->view->listTips = $mdlTips->getList( "MONTH(`date`) = " . $date->get("M") . " AND `result` IS NOT NULL ", "date DESC", 1, 20 )->toArray ();
	}

	public function paymentPlansAction()
	{

	}

	public function tipRecordsAction()
	{
		$date = new Zend_Date();
		$this->view->date = $date;
			
		$mdlTips = new Model_Tips();
		$this->view->tipRecords = $mdlTips->getTipRecords();
	}

	public function tipRecordsDetailAction()
	{
		$date = new Zend_Date();
		$this->view->date = $date;
			
		$mdlTips = new Model_Tips();
		$this->view->tipRecords = $mdlTips->getList();
	}

	public function registrationAction()
	{
		// Set no error
		$this->view->error = 0;

		if($this->_request->isPost())
		{
			$postData = $this->_request->getPost();
			foreach($postData as &$param) {
				$param = stripslashes(trim($param));
			}
				
			// Create user model
			$usrModel = new Model_Members();

			if( $usrModel->numRows(array('username = ?'=>$postData['username'])) || !$postData['username'])
			{
				$this->view->error = 1;
			}
			elseif ( $usrModel->numRows(array('email = ?'=>$postData['email'])) || !$postData['email'])
			{
				$this->view->error = 2;
			}
			else
			{

				$user = $usrModel->fetchNew();
				$user->username = $postData['username'];
				$user->password = md5($postData['password']);
				$user->email = $postData['email'];
				$user->active = 1;

				$dateObject = Zend_Date::now ();
				$user->created_ts = $dateObject->toString("YYYY-MM-dd HH:mm:ss");
				$user->save();

				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('home', 'site');
				/*
				 $arr_key = array('gender_id', 'age', 'phone', 'occupation', 'city', 'country_id', 'religion_id', 'knowledge_id', 'smoking_id', 'drinking_id',
				 'body_id', 'height_id', 'weight_id', 'married_id', 'can_dancing', 'can_sing', 'favourite_musician', 'favourite_music',
				 'favourite_band', 'favourite_sport', 'favourite_subject', 'talking_freetime', 'yourseft', 'finding_style', 'target_finding');
				 foreach($postData as $key=>$value)
				 {
					if(in_array($key,$arr_key) && $key && $value!='')
					{
					$user->$key = $value;
					}
					}

					$NMC = NMC::getInstance();
					$upload = $NMC->uploadUserAvatar('image', true);
					if(!$upload['error'])
					{
					$user->image = $upload['value'];
					} else {
					$this->_redirector = $this->_helper->getHelper('Redirector');
					$this->_redirector->gotoSimple('registration', 'index');
					}


					$user->save();

					//$NMC = NMC::getInstance();
					//$NMC->mail_acitivecode($postData['usrname'], $activecode, $postData['email']);


					*/
			}
		}
	}


	public function loginAction()
	{
		if($this->_request->isPost())
		{
			$postData = $this->_request->getPost();
			
			//set up the auth adapter
			//get the default db adapter
			$db = Zend_Db_Table::getDefaultAdapter ();

			//create the auth adapter
			$authAdapter = new Zend_Auth_Adapter_DbTable ( $db, 'members', 'username', 'password' );

			//set the username and password
			$authAdapter->setIdentity ( $postData ['username'] );
			$authAdapter->setCredential ( md5 ( $postData ['password'] ) );

			//authenticate
			$result = $authAdapter->authenticate ();
			if ($result->isValid ()) {
				// store the username, first and last names of the user
				$auth = Zend_Auth::getInstance ();
				$storage = $auth->getStorage ();
				$object = $authAdapter->getResultRowObject ( array ('id', 'username'));
				$object->role = 'member';
				
				$storage->write ( $object );
				return $this->_redirect ( 'member/black-market' );
			} else {
				$this->view->loginMessage = "Sorry, your username or password was incorrect";
			}
		}
	}

	public function logoutAction()
    {
        $authAdapter = Zend_Auth::getInstance ();
        $authAdapter->clearIdentity ();
        
        return $this->_redirect ( 'site/home' );
    }

	public function lostPasswordAction()
	{
			
	}

	public function faqAction()
	{
			
	}

	public function contactAction()
	{
		$frmContact = new Form_Site_Contact ( );
		if ($this->_request->isPost () && $frmContact->isValid ( $_POST ))
		{
			// get the posted data
			$sender = $frmContact->getValue ( 'name' );
			$email = $frmContact->getValue ( 'email' );
			$subject = $frmContact->getValue ( 'subject' );
			$message = $frmContact->getValue ( 'message' );

			$mail = new CMS_Mail();
			$mail->setTemplate('contact');
			$mail->sender = $sender;
			$mail->email = $email;
			$mail->subject = $subject;
			$mail->message = $message;

			$mail->setRecipient('nmcan1306@yahoo.com');
			$mail->send();

			// inform the view with the status
			$this->view->messageProcessed = true;
		}
		$frmContact->setAction ( $this->_helper->url ( 'contact', 'site' ) );
		$this->view->form = $frmContact;
	}
}